<template>
  <div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="借出登记" name="first"><newBorrowOrder ref="newBorrowOrder" @confirm="confirmBorrow" @change="change"/></el-tab-pane>
      <el-tab-pane label="借出归还" name="second"><newReturnOrder ref="newReturnOrder" @confirm="confirmReturn" @change="change"/></el-tab-pane>
    </el-tabs>
    <div class="btnWrap">
      <el-button type="primary" @click="closeDrawer">取消</el-button>
      <el-button :loading="loading" type="primary" @click="saveHandler">保存</el-button>
      <el-button :loading="loading" type="primary" @click="account" :disabled="accountAble" :style="{ display: 'none' }">>{{ buttonValue }}</el-button>
      <el-button :loading="loading" type="primary" @click="print" :style="{ display: 'none' }">>打印</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import _ from 'lodash';
import { confirmAction, notifyAction } from '@/utils';
import newBorrowOrder from './newBorrowOrder';
import newReturnOrder from './newReturnOrder';
import { saveBorrowOrder, saveReturnOrder, deleteBorrowOrder } from '@/api/part/tools';

export default {
  mixins: [dictionary],
  components: {
    newBorrowOrder,
    newReturnOrder
  },
  data() {
    return {
      activeName: 'first',
      buttonValue: '出库',
      accountAble: true,
      loading: false
    };
  },
  methods: {
    handleClick(tab, event) {
      if (this.activeName == 'first') {
        this.buttonValue = '出库';
        this.accountAble = true;
      } else {
        this.buttonValue = '入库';
        this.accountAble = true;
      }
    },
    closeDrawer() {
      this.$emit('close');
    },
    confirmReturn(val) {
      console.log('returnval::', val);
    },
    confirmBorrow(val) {
      console.log('borrowval::', val);
    },
    async saveHandler(val) {
      this.loading = true;
      console.log('saveVal', val);
      let res = '';
      if (this.buttonValue == '出库') {
        let array = await this.$refs.newBorrowOrder.$refs.topFilter.GET_FORM_DATA();
        let param = array[1];
        console.log('GET_FORM_DATA', array);
        console.log('GET_FORM_DATA11', array[1]);
        param.itemList = this.$refs.newBorrowOrder.syncTableData;
        param.deleteList = [];
        let quantityFlag = false;
        param.itemList.find(x => {
          if (x.counts <= 0) {
            quantityFlag = true;
          }
        });
        if (quantityFlag) {
          this.$notify({
            title: '失败',
            message: '借出数量不能为0',
            type: 'warning'
          });
          this.loading = false;
          return;
        }
        console.log('this.$refs.newBorrowOrder.', this.$refs.newBorrowOrder);
        if (!param.borrowUser) {
          this.$notify({
            title: '失败',
            message: '请输入借用人',
            type: 'warning'
          });
          this.loading = false;
          return;
        }
        if (!param.itemList || param.itemList.length <= 0) {
          this.$notify({
            title: '失败',
            message: '请添加工具',
            type: 'warning'
          });
          this.loading = false;
          return;
        }
        res = await saveBorrowOrder(param);
      }
      if (this.buttonValue == '入库') {
        let array = await this.$refs.newReturnOrder.$refs.topFilter.GET_FORM_DATA();
        let param = array[1];
        console.log('GET_FORM_DATA', array);
        console.log('GET_FORM_DATA11', array[1]);
        if (_.isNil(param.borrowNo)) {
          this.$notify({
            title: '失败',
            message: '请输入借用单号',
            type: 'warning'
          });
          this.loading = false;
          return;
        }
        let flag = false;
        let quantityFlag = false;
        param.deleteList = [];
        param.itemList = this.$refs.newReturnOrder.syncTableData;
        param.itemList.find(x => {
          if (x.counts > x.maxCounts) {
            flag = true;
          }
          if (x.counts <= 0) {
            quantityFlag = true;
          }
        });
        if (quantityFlag) {
          this.$notify({
            title: '失败',
            message: '归还数量不能为0',
            type: 'warning'
          });
          this.loading = false;
          return;
        }
        if (flag) {
          this.$notify({
            title: '失败',
            message: '归还数量超过借入数量',
            type: 'warning'
          });
          this.loading = false;
          return;
        }
        res = await saveReturnOrder(param);
        this.closeDrawer();
      }
      if (res.resultCode == 200) {
        this.$notify({
          title: '成功',
          message: '保存成功',
          type: 'success'
        });
        this.accountAble = false;
      }
      this.loading = false;
      this.closeDrawer();
    },
    account() {
      console.log('account:::');
      this.loading = true;
      this.loading = false;
    },
    print() {
      this.loading = true;
      this.loading = false;
    },
    change() {
      this.accountAble = true;
    }
  }
};
</script>

<style lang="less" scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
</style>
